package com.cykj.controller;

import com.cykj.service.AdminService;
import com.cykj.util.ImageCodeUtils;
import com.cykj.util.ResponseDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@RestController
@RequestMapping("/admin")
public class AdminController {
    private static String rightCode;
    @Autowired
    private AdminService adminService;

    @RequestMapping("/login")
    public ResponseDto login(String name, String pwd, String cardId, String code) {
        // 先判断验证码是否正确，然后再进行账号密码的判断
        if (code.equals(rightCode)) {
            return adminService.login(name, pwd, cardId);
        } else {
            return new ResponseDto(2, "验证码错误", null);
        }
    }

    @RequestMapping("/createCode")
    public void createCode(HttpServletResponse response, HttpSession session) {
        // 生成验证码图片的工具类
        ImageCodeUtils imageCodeUtils = new ImageCodeUtils();
        rightCode = imageCodeUtils.getCode();
        System.out.println("rightCode1"+rightCode);
        try {
            imageCodeUtils.write(response.getOutputStream());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    @RequestMapping("/register")
    public ResponseDto register(String name,String pwd,String cardId,String code){
        System.out.println("rightCode2"+rightCode);
        System.out.println(code);
        // 先判断验证码是否正确，然后再进行账号密码的判断
        if (code.equals(rightCode)) {
            return adminService.register(name, pwd, cardId);
        } else {
            return new ResponseDto(2, "验证码错误", null);
        }

    }
    @RequestMapping("/getUsers")
    public ResponseDto getUsers(int pageCurr,int pageSize){
        return adminService.getUsers(pageCurr, pageSize);
    }
    @RequestMapping("/getScenic")
    public ResponseDto getScenic(int pageCurr,int pageSize) {
        return adminService.getScenic(pageCurr, pageSize);
    }
    @RequestMapping("/getTicketOrders")
    public ResponseDto getTicketOrders(int pageCurr,int pageSize) {
        return adminService.getTicketOrders(pageCurr, pageSize);
    }
    @RequestMapping("/alterPwd")
    public ResponseDto alterPwd(int id, String newPwd) {
        return adminService.alterPwd(id, newPwd);
    }
}
